package com.wlstock.funi.model;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.wlstock.funi.utils.CaculateUtils;
import com.wlstock.funi.utils.TimeUtils;

import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 我的持仓表
 * @author admin
 *
 */
@DatabaseTable(tableName = "MyAccountStocks")
public class MyAccountStocks extends BaseModel{
	private static final int DEFAULT_KEEP_DAYS = 888;
	@DatabaseField (id = true)//股票代码
	private String stkCode;
	
	@DatabaseField//股票名
	private String stkName;
	
	@DatabaseField//可用数量
	private int  stkQty;

	@DatabaseField//买入价格
	private String costPrice;
	
	@DatabaseField//现价
	private String curPrice;
	
	@DatabaseField//收益
	private float profitRate;
	
	@DatabaseField//最高收益
	private float hProfiteRate = 0;
	
	@DatabaseField//利润价格
	private float profitMoney;
	
	@DatabaseField//目标价格
	private float targetPrice;

	@DatabaseField
	private int fundId;
	
	@DatabaseField
	private String isMeOwner;
	
	@DatabaseField
	private String isAIOwner;
	
	@DatabaseField
	private Date tradeTime;
	
	@DatabaseField
	private long linkId;
	
	public String isRecord = "Y";

	@DatabaseField//服务器交易日期
	private String serverTradeDate;

	@DatabaseField//服务器交易时间
	private String serverTradeTime;

	@DatabaseField//持有时长
	private String keepDaysCount;

	@DatabaseField//目标持有时间
	private int targetDaysCount;

	@DatabaseField
	private  String buyReason;

	@DatabaseField
	private Date lastUpdate = new Date();

	@DatabaseField//成本(总的)
	private float cost;

	@DatabaseField//市值
	private float marketPrice;

	public MyAccountStocks(String stkCode, String stkName, int stkQty,
						   String costPrice, String curPrice, float cost,
						   float marketPrice, float profitRate, float profitMoney) {
		super();
		setFileds(stkCode, stkName, stkQty, costPrice, curPrice, cost, marketPrice, profitRate, profitMoney);
	}

	public MyAccountStocks(String[] queryResult) {
		super();
		if (queryResult!=null && queryResult.length==2){
			this.stkCode = queryResult[0];
			this.stkName = queryResult[1];
		}
	}


	public void setFileds(String stkCode, String stkName, int stkQty, String costPrice, String curPrice, float cost, float marketPrice, float profitRate, float profitMoney) {
		this.stkCode = stkCode;
		this.stkName = stkName;
		this.stkQty = stkQty;
		this.costPrice = costPrice;
		this.curPrice = curPrice;
		this.cost  = cost;
		this.marketPrice = marketPrice;
		this.profitRate = profitRate/100;
		this.profitMoney = profitMoney;
	}


	public void setWithHisData(Elements items){
		this.serverTradeDate = ((TextNode)items.get(0).childNode(0)).text().toString().trim();//委托日期
		this.serverTradeTime = ((TextNode)items.get(1).childNode(0)).text().toString().trim();//时间
		this.stkCode         = ((TextNode)items.get(3).childNode(0)).text().toString().trim();//代码
		this.costPrice       = ((TextNode)items.get(6).childNode(0)).text().toString().trim();//成交价格
		if (this.tradeTime == null){
			try {
				this.tradeTime = new SimpleDateFormat("yyyy-MM-dd").parse(this.serverTradeDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (this.stkQty!=0){
			this.keepDaysCount = getKeepDaysCount();
		}else{
			this.keepDaysCount = "0";
		}

		this.targetDaysCount = DEFAULT_KEEP_DAYS;
	}

	public  MyAccountStocks() {}
	
	public String getStkCode() {
		return stkCode;
	}

	public void setStkCode(String stkCode) {
		this.stkCode = stkCode;
	}

	public String getStkName() {
		return stkName;
	}

	public void setStkName(String stkName) {
		this.stkName = stkName;
	}

	public int getStkQty() {
		return stkQty;
	}

	public void setStkQty(int stkQty) {
		this.stkQty = stkQty;
	}

	public String getCostPrice() {
		return costPrice;
	}

	public void setCostPrice(String costPrice) {
		this.costPrice = costPrice;
	}

	public String getCurPrice() {
		return curPrice;
	}

	public void setCurPrice(String curPrice) {
		this.curPrice = curPrice;
	}

	public float getProfitRate() {
		return profitRate;
	}

	public void setProfitRate(float profitRate) {
		this.profitRate = profitRate;
	}

	public float getProfitMoney() {
		return profitMoney;
	}

	public void setProfitMoney(float profitMoney) {
		this.profitMoney = profitMoney;
	}

	public int getFundId() {
		return fundId;
	}

	public void setFundId(int fundId) {
		this.fundId = fundId;
	}

	public float gethProfiteRate() {
		return hProfiteRate;
	}

	public void sethProfiteRate(float hProfiteRate) {
		this.hProfiteRate = hProfiteRate;
	}

	public String getIsMeOwner() {
		return isMeOwner;
	}

	public void setIsMeOwner(String isMeOwner) {
		this.isMeOwner = isMeOwner;
	}

	public String getIsAIOwner() {
		return isAIOwner;
	}

	public void setIsAIOwner(String isAIOwner) {
		this.isAIOwner = isAIOwner;
	}

	public Date getTradeTime() {
		return tradeTime;
	}

	public void setTradeTime(Date tradeTime) {
		this.tradeTime = tradeTime;
	}

	public long getLinkId() {
		return linkId;
	}

	public void setLinkId(long linkId) {
		this.linkId = linkId;
	}

	public String getServerTradeDate() {
		return serverTradeDate;
	}

	public void setServerTradeDate(String serverTradeDate) {
		this.serverTradeDate = serverTradeDate;
	}

	public String getKeepDaysCount() {
		String nowDateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		if (serverTradeDate!=null){
			long count = TimeUtils.dateDiff(serverTradeDate, nowDateStr);
			return String.valueOf(count);
		}else{
			long count = TimeUtils.dateDiff(nowDateStr, nowDateStr);
			return String.valueOf(count);
		}

	}

	public int getTargetDaysCount() {
		return targetDaysCount;
	}

	public void setTargetDaysCount(int targetDaysCount) {
		this.targetDaysCount = targetDaysCount;
	}
	
	public String highProfitStr(){
		return " 持仓最高:" + CaculateUtils.getPercentByFloat(hProfiteRate);
	}

	public String curProfitStr(){
		return  CaculateUtils.getPercentByFloat(profitRate);
	};

	public String curPrice(){
		return "现价:" + getCurPrice();
	};

	public String getServerTradeTime() {
		return serverTradeTime;
	}

	public void setServerTradeTime(String serverTradeTime) {
		this.serverTradeTime = serverTradeTime;
	}

	public String nameWithCode(){
		return stkName +"("+stkCode+") ";
	}

	public String serverTradeDate() {
		return "交易日期:" + serverTradeDate!=null? serverTradeDate:"未知日期";
	}

	public String keepDateCountStr(){
		return "持股天数(不含周末):" + getKeepDaysCount();
	}

	private String getBuyReason() {
		if (buyReason!=null){
			return "原因:"+buyReason;
		}
		return "原因:无";
	}

	private void setStraMaxkeepDays(StrategyFundModel fundModel){
		if (fundModel == null)
			return;
		this.targetDaysCount = fundModel.getHighYearKeepDays();
	}

	private void  setStraBuyReson(StrategyFundModel fundModel){
		if (fundModel==null)
			return;
		this.buyReason = "策略:最高年化"+ CaculateUtils.getPercentByFloat(fundModel.getHighYearPecentage())
				+" 持仓("+fundModel.getHighYearKeepDays()+"天)"
				+"最高成功率"+ CaculateUtils.getPercentByFloat(fundModel.getMaxSuccessPecentage())
				+" 持仓("+fundModel.getMaxSuccessKeepDays()+"天)";
	}

	public void setWithStraModel(StrategyFundModel fundModel){
		this.setStraMaxkeepDays(fundModel);
		this.setStraBuyReson(fundModel);
	}

	private String getTargetDaysCountStr() {
		return "应该持仓:"+getTargetDaysCount()+"天";
	}

	public String getTargetDaysCountStr(StrategyFundModel fundModel){
		this.setStraMaxkeepDays(fundModel);
		return getTargetDaysCountStr();
	}

	public String getBuyReason(StrategyFundModel fundModel){
		this.setStraBuyReson(fundModel);
		return getBuyReason();
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public float getCost() {
		return cost;
	}

	public void setCost(float cost) {
		this.cost = cost;
	}

	public float getMarketPrice() {
		return marketPrice;
	}

	public void setMarketPrice(float marketPrice) {
		this.marketPrice = marketPrice;
	}

	@Override
	public String toString() {
		return "持仓时间"+getKeepDaysCount()+"目标持仓时间:"+getTargetDaysCount()+"买入原因:"+getBuyReason();
	}
}
